<?php
/**
 * Created by PhpStorm.
 * User: FDG367
 * Date: 2017/11/7
 * Time: 17:32
 */
namespace console\controllers;


use common\helps\tools;
use common\models\Coupon;
use common\models\CouponPlus;
use common\models\CouponReceive;
use Yii;
use yii\console\Controller;
use common\helps\Wechat;
class SendMsgController extends Controller{
	const FOUR_WEEK		= 4;//第四周
	const TWO_WEEK		= 2;//第二周
	const WEEK_DAY_FOUR	= 4;//星期四

	const RECEIVE_TITLE = '您的兑换码即将到期，请您注意。';
	
	const SHOP_NAME = '剪刀吴中店';
// 	const RECEIVE_CONTENT = '好优惠的优惠券';

	/**
	 * @param 通过微信号在每个月第四周星期四发送消息未领取优惠券
	 * @return view
	 */
	public function actionSendWechatMsg(){
	    set_time_limit(0);
		//当月的第一天是周几
		$extWeek		=date('w',strtotime(date('Y-m-01', time())));
		$_SERVER['REMOTE_ADDR']='127.0.0.1';//设置当前ip

		//计算当天是否是当月的第四个周的周四
		if( self::TWO_WEEK == ceil((date('d')+ $extWeek)/7) && self::WEEK_DAY_FOUR == date('w')) {
			//微信消息模板推送 TODO
			$ReceiveInfo	=CouponReceive::getNoExchange();

			//发送模板
			if($countInfo	=count($ReceiveInfo) > 0){
			    $send_user = [];
			    foreach ($ReceiveInfo as $receive_val){
			        $send_user[$receive_val['wx_openid']][] = $receive_val;
			    }
				$i 			= 0;
				if (is_array($send_user) && count($send_user) > 0)foreach ($send_user as $wx_openid=>$receive_count){
				    $sms_verification = array_column($receive_count, 'sms_verification');
				    $coupon_end_time_arr = array_column($receive_count, 'coupon_end_time');
				    $coupon_end_time = min($coupon_end_time_arr);
				    $week = date('N', strtotime($coupon_end_time));
				    $week_name = tools::getWeek(intval($week));
				    $date = date('Y年m月d日',strtotime($coupon_end_time));
				    $content = '';
				    if (is_array($receive_count) && count($receive_count) > 0)foreach ($receive_count as $receive_val){
				        if ($coupon_end_time == $receive_val['coupon_end_time']){
				            $content = $receive_val['content'];
				        }
				    }
				    $message_data = [
				        'keyword1'=>self::SHOP_NAME,
				        'keyword2'=>implode(',', $sms_verification),
				        'keyword3'=>$content,
				        'keyword4'=>$date.' '.$week_name,
				    ];
				    $title = self::RECEIVE_TITLE;
				    $count = count($receive_count);
				    $return = Wechat::sendWechatMessage($wx_openid,$title,$count,$message_data);
				    if(100==$i){
				        sleep(1);
				    }
				    $i++;
				}
			}
		}
	}


	/**
	 * @param 通过短信在每个月第四周星期四发送消息未领取优惠券
	 * @return view
	 */
	public function actionSendMsg(){
		set_time_limit(0);
		//获取马上过期的优惠券信息
		$_SERVER['REMOTE_ADDR']='127.0.0.1';//设置当前ip
		//当月的第一天是周几
		$extWeek	=date('w',strtotime(date('Y-m-01', time())));

		//计算当天是否是当月的第四个周的周四
		if( self::FOUR_WEEK == ceil((date('d')+ $extWeek)/7) && self::WEEK_DAY_FOUR == date('w')) {
			//获取要发送的信息
			$ReceiveInfo	= CouponReceive::getSmsNoExchange();

			if($ReceiveInfo && count($ReceiveInfo) >0){
				foreach($ReceiveInfo as $key=>$val){

					//发送优惠券短信 短信错误有记录 省略
					tools::sendMobileCode($val['mobile'],$val['num'],'sms_coupon_push');

					if(0==($key+1)%100){
						sleep(1);	//100条停顿一秒
					}
				}
			}


		}
	}

}